package com.visec.hdy.sync.ftp.code;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.visec.hdy.sync.ftp.config.SyncFileMsg;
import com.visec.hdy.sync.ftp.config.SyncFileMsgCV;
import com.visec.hdy.sync.ftp.config.TransmissionAction;
import com.visec.hdy.transmission.socket.code.SocketClient;

public class SyncAccept extends Thread {
	private SocketClient socketClient;
	private Map<String, GetManager> fgm;

	public SyncAccept(Map<String, GetManager> fgm, SocketClient socketClient) {
		super();
		this.socketClient = socketClient;
		this.fgm = fgm;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		GetManager ftpGetManager = null;
		while (!isInterrupted()) {
			try {
				SyncFileMsg ftpFileMsg = SyncAccept.getActionMsg(socketClient);
				if (ftpFileMsg == null) {
					break;
				}

				ftpGetManager = fgm.get(ftpFileMsg.getFtpId());

				if (ftpGetManager == null) {
					socketClient.write("close");
					break;
				}
				if (ftpFileMsg.getPrefix() == TransmissionAction.check_sync_) {
					ftpGetManager.resetData();
					String checkFile = ftpGetManager.checkFile(ftpFileMsg);
					socketClient.write(checkFile);
					while (true) {
						SyncFileMsg actionMsg = getActionMsg(socketClient);
						if (actionMsg != null) {
							String check = ftpGetManager.checkFile(actionMsg);
							socketClient.write(check);
						} else {
							break;
						}
					}
					break;
				} else if (ftpFileMsg.getPrefix() == TransmissionAction.sync_) {
					String syncFile = ftpGetManager.syncFile(ftpFileMsg, socketClient.getInput());
					socketClient.write(syncFile);
				}
			} catch (Exception e) {
				// TODO: handle exception
				break;
			}
		}
		try {
			if (ftpGetManager != null)
				ftpGetManager.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			socketClient.close();
			DispatchAnswer.redI();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	// static Logger l = Logger.getLogger(SyncAccept.class);

	public static SyncFileMsg getActionMsg(SocketClient socketClient) {
		byte[] b = new byte[2048];
		int a = 0;
		String ftpFileMsg = "";

		while ((a = socketClient.read(b)) > 0) {
			ftpFileMsg = ftpFileMsg + new String(b, 0, a, socketClient.getCharset());
			if (ftpFileMsg.indexOf(TransmissionAction._end.toString()) > -1)
				break;
		}
		if (ftpFileMsg == null || ftpFileMsg.equals("")) {
			return null;
		}
		//System.out.println(ftpFileMsg);
		
		ftpFileMsg = ftpFileMsg.substring(0, ftpFileMsg.indexOf(TransmissionAction._end.toString()));
		String[] split = ftpFileMsg.split("\";");
		Map<String, String> dataMap = new HashMap<String, String>();
		for (String s : split) {
			String[] split2 = s.split("=\"");
			if (split2.length > 1) {
				dataMap.put(split2[0], split2[1]);
			}
		}
		SyncFileMsgCV ffmcv = new SyncFileMsgCV();
		ffmcv.setParams(dataMap);
		SyncFileMsg parameter = ffmcv.getParameter();
		return parameter;
	}

}
